package com.ruoyi.web.controller.oper;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.oper.domain.*;
import com.ruoyi.oper.service.IOperCartService;
import com.ruoyi.oper.service.IOperOrderService;
import com.ruoyi.oper.service.IOperProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/oper/order")
public class OperOrderController extends BaseController {
    @Autowired
    private IOperOrderService operOrderService;
    @Autowired
    private IOperCartService operCartService;
    @Autowired
    private IOperProductService operProductService;
    @GetMapping("/insert_order")
    public boolean insertOrder(OperOrder operOrder){
        operOrderService.insertOperOrder(operOrder);
        Integer orderId = operOrderService.getOrderId();
        OperCart operCart = new OperCart();
        operCart.setPersonId(operOrder.getPersonId());
        List<OperCart> operCartList = operCartService.selectOperCartList(operCart);
        List<OperItem> operItemList = new ArrayList<>();
        for(OperCart oc : operCartList){
            OperItem operItem = new OperItem();
            operItem.setProductId(oc.getProductId());
            operItem.setYanseId(oc.getYanseId());
            operItem.setChimaId(oc.getChimaId());
            operItem.setProCount(oc.getProCount());
            operItem.setOrderId(orderId);
            operItemList.add(operItem);
        }
        for(OperItem oi : operItemList){
            operOrderService.insertOperItem(oi);
            OperProitems operProitems = new OperProitems();
            operProitems.setProductId(oi.getProductId());
            operProitems.setYanseId(oi.getYanseId());
            operProitems.setChimaId(oi.getChimaId());
            List<OperProitems> operProitemsList = operProductService.selectOperProitemsList(operProitems);
            Integer proCount = operProitemsList.get(0).getProCount();
            operProitems.setProCount(proCount-oi.getProCount());
            operProitems.setId(operProitemsList.get(0).getId());
            operProductService.updateProitemsExample(operProitems);
        }
        for(OperCart oc : operCartList){
            operCartService.deleteOperCartById(oc.getId());
        }
        return true;
    }

    @GetMapping("/get_oper_order_list")
    public List<OperOrder> getOperOrderList(OperOrder operOrder){
        List<OperOrder> operOrderList = operOrderService.selectOperOrderList(operOrder);
        if(operOrderList != null && operOrderList.size() > 0){
            for(OperOrder o : operOrderList){
                OperItem operItem = new OperItem();
                operItem.setOrderId(o.getId());
                List<OperItem> operItemList = operOrderService.selectOperItemList(operItem);
                List<OperItemVO> operItemVOList = new ArrayList<>();
                for(OperItem opi : operItemList){
                    OperProduct op = operProductService.selectProductById(opi.getProductId());
                    OperYanse oy = operProductService.selectYanseById(opi.getYanseId());
                    OperChima oa = operProductService.selectChimaById(opi.getChimaId());
                    OperItemVO operItemVO = new OperItemVO(op,oy,oa,opi);
                    operItemVOList.add(operItemVO);
                }
                o.setOperItemVOList(operItemVOList);
            }
        }
      return operOrderList;
    }
}
